package cn.MaySoon.shenfu;

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;

public class jineng {

        public static String 神符技能激活书() {
            return "§a§0§b§3§e§l神符技能激活书";
        }
        public static String 神符技能升级书() {
        return "§a§0§b§3§6§l神符技能升级书";
        }


    public static Boolean upLevelPlayer防御(Player p) throws IOException {
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        int level = data.getInt("jineng.防御");
        int nextlevel = level+1;
        int maxlevel = main.config.getInt("jineng.防御.maxlevel");
        if (level==maxlevel){
            p.sendMessage("§c已经达到最大等级");
            return false;
        }
        //学习
        if (level==0){
            int jie =  main.PlayerShenFuData.get(p).getPlayerjie();
            if (jie<2){
                p.sendMessage("§c学习失败,神符未到指定阶级");
                return false;
            }

            if (!takeitem(p,神符技能激活书(),1)){
                p.sendMessage("§c学习失败,背包没有神符技能激活书");
                return false;
            }
            data.set("jineng.防御",1);
            data.save(file);
            p.sendMessage("§a学习技能成功,属性已激活");
            jinengap.updateAttribute(p);
            return  true;
        }

        int needItemAmount =main.config.getInt("jineng.防御."+nextlevel+".need");
        if (!takeitem(p,神符技能升级书(),needItemAmount)){
            p.sendMessage("§c升级失败,背包神符技能升级书道具不足");
            return false;
        }
        data.set("jineng.防御",nextlevel);
        data.save(file);
        jinengap.updateAttribute(p);
        return true;

    }

    public static Boolean upLevelPlayer生命(Player p) throws IOException {
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        int level = data.getInt("jineng.生命");
        int nextlevel = level+1;
        int maxlevel = main.config.getInt("jineng.生命.maxlevel");
        if (level==maxlevel){
            p.sendMessage("§c已经达到最大等级");
            return false;
        }
        //学习
        if (level==0){
            int jie = main.PlayerShenFuData.get(p).getPlayerjie();
            if (jie<3){
                p.sendMessage("§c学习失败,神符未到指定阶级");
                return false;
            }
            if (data.getInt("jineng.防御")<1){
                p.sendMessage("§c学习失败,神符防御技能未达到一级");
                return false;
            }
            if (!takeitem(p,神符技能激活书(),1)){
                p.sendMessage("§c学习失败,背包没有神符技能激活书");
                return false;
            }
            data.set("jineng.生命",1);
            data.save(file);
            p.sendMessage("§a学习技能成功,属性已激活");
            jinengap.updateAttribute(p);
            return  true;
        }

        int needItemAmount =main.config.getInt("jineng.生命."+nextlevel+".need");
        if (!takeitem(p,神符技能升级书(),needItemAmount)){
            p.sendMessage("§c升级失败,背包神符技能升级书道具不足");
            return false;
        }
        data.set("jineng.生命",nextlevel);
        data.save(file);
        jinengap.updateAttribute(p);
        return true;

    }




    public static Boolean upLevelPlayer攻击(Player p) throws IOException {
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        int level = data.getInt("jineng.攻击");
        int nextlevel = level+1;
        int maxlevel = main.config.getInt("jineng.攻击.maxlevel");
        if (level==maxlevel){
            p.sendMessage("§c已经达到最大等级");
            return false;
        }
        //学习
        if (level==0){
            int jie = main.PlayerShenFuData.get(p).getPlayerjie();
            if (jie<4){
                p.sendMessage("§c学习失败,神符未到指定阶级");
                return false;
            }
            if (data.getInt("jineng.生命")<1){
                p.sendMessage("§c学习失败,神符生命技能未达到一级");
                return false;
            }
            if (!takeitem(p,神符技能激活书(),1)){
                p.sendMessage("§c学习失败,背包没有神符技能激活书");
                return false;
            }
            data.set("jineng.攻击",1);
            data.save(file);
            p.sendMessage("§a学习技能成功,属性已激活");
            jinengap.updateAttribute(p);

            return  true;
        }

        int needItemAmount =main.config.getInt("jineng.攻击."+nextlevel+".need");
        if (!takeitem(p,神符技能升级书(),needItemAmount)){
            p.sendMessage("§c升级失败,背包神符技能升级书道具不足");
            return false;
        }
        data.set("jineng.攻击",nextlevel);
        data.save(file);
        jinengap.updateAttribute(p);
        return true;

    }

    public static Boolean upLevelPlayer高级生命(Player p) throws IOException {
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        int level = data.getInt("jineng.高级生命");
        int nextlevel = level+1;
        int maxlevel = main.config.getInt("jineng.高级生命.maxlevel");
        if (level==maxlevel){
            p.sendMessage("§c已经达到最大等级");
            return false;
        }
        //学习
        if (level==0){
            int jie = main.PlayerShenFuData.get(p).getPlayerjie();
            if (jie<4){
                p.sendMessage("§c学习失败,神符未到指定阶级");
                return false;
            }
            if (data.getInt("jineng.高级攻击")<1){
                p.sendMessage("§c学习失败,神符攻击技能未达到一级");
                return false;
            }
            if (!takeitem(p,神符技能激活书(),1)){
                p.sendMessage("§c学习失败,背包没有神符技能激活书");
                return false;
            }
            data.set("jineng.高级生命",1);
            data.save(file);
            p.sendMessage("§a学习技能成功,属性已激活");
            jinengap.updateAttribute(p);
            return  true;
        }

        int needItemAmount =main.config.getInt("jineng.高级生命."+nextlevel+".need");
        if (!takeitem(p,神符技能升级书(),needItemAmount)){
            p.sendMessage("§c升级失败,背包神符技能升级书道具不足");
            return false;
        }
        data.set("jineng.高级生命",nextlevel);
        data.save(file);
        jinengap.updateAttribute(p);
        return true;

    }

    public static Boolean upLevelPlayer大师(Player p) throws IOException {
        File file = new File(main.plugin.getDataFolder() + "/PlayerData/" + p.getName() + ".yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(file);
        int level = data.getInt("jineng.大师");
        int nextlevel = level+1;
        int maxlevel = main.config.getInt("jineng.大师.maxlevel");
        if (level==maxlevel){
            p.sendMessage("§c已经达到最大等级");
            return false;
        }
        //学习
        if (level==0){
            int jie = main.PlayerShenFuData.get(p).getPlayerjie();
            if (jie<5){
                p.sendMessage("§c学习失败,神符未到指定阶级");
                return false;
            }
            if (data.getInt("jineng.高级生命")<1){
                p.sendMessage("§c学习失败,神符高级生命技能未达到一级");
                return false;
            }
            if (!takeitem(p,神符技能激活书(),1)){
                p.sendMessage("§c学习失败,背包没有神符技能激活书");
                return false;
            }
            data.set("jineng.大师",1);
            data.save(file);
            p.sendMessage("§a学习技能成功,属性已激活");
            jinengap.updateAttribute(p);
            return  true;
        }

        int needItemAmount =main.config.getInt("jineng.大师."+nextlevel+".need");
        if (!takeitem(p,神符技能升级书(),needItemAmount)){
            p.sendMessage("§c升级失败,背包神符技能升级书道具不足");
            return false;
        }
        data.set("jineng.大师",nextlevel);
        data.save(file);
        jinengap.updateAttribute(p);
        return true;

    }


    public static  boolean takeitem (Player p ,String Name ,int howmany) {
        Inventory inv = p.getInventory();
        int tocalamo = 0;
        int Max = inv.getSize() - 1;
        for (int i = 0; i < Max; i++)
        {
            ItemStack item = inv.getItem(i);
            if (item != null) {
                if (item.hasItemMeta()) {
                    if (item.getItemMeta().hasDisplayName()) {
                        if (item.getItemMeta().getDisplayName().equalsIgnoreCase(Name)) {
                            tocalamo =tocalamo+item.getAmount();

                        }



                    }
                }
            }
        }

        if (tocalamo>=howmany) {
            take(p, Name, howmany);
            return true;
        }

        return false;
    }



    public static  void take(Player p, String Name, int amo) {
        Inventory playerInventory = p.getInventory();
        int Max = playerInventory.getSize() - 1;
        int takeamo = amo;
        for (int i = 0; i < Max; i++) {

            ItemStack item = playerInventory.getItem(i);
            if (item != null &&
                    item.hasItemMeta() &&
                    item.getItemMeta().hasDisplayName() &&

                    item.getItemMeta().getDisplayName().equalsIgnoreCase(Name) &&
                    item.getAmount() > 0)
                if (takeamo != 0) {
                    if (item.getAmount() == 1) {
                        p.getInventory().setItem(i, null);
                        takeamo--;
                    }
                    else {

                        if (item.getAmount() - takeamo >= 0) {
                            item.setAmount(item.getAmount() - takeamo);
                            p.getInventory().setItem(i, item);

                            break;
                        }
                        p.getInventory().setItem(i, null);
                        takeamo -= item.getAmount();
                    }
                } else {
                    break;
                }
        }
    }
}
